﻿using System.Collections;
using System.Windows;
using System.Windows.Controls;
using Microsoft.SilverlightMediaFramework.Player;
using System.Collections.Generic;
using SMF.HowTo.Shared.Extensions;

namespace SMF.HowTo.Player
{
    [TemplatePart(Name = VideoInfoListElementName, Type = typeof (ItemsControl))]
    public class HowToPlayer : Microsoft.SilverlightMediaFramework.Player.Player
    {
        private const string VideoInfoListElementName = "VideoInfoList";

        public static readonly DependencyProperty SelectedMarkerProperty =
            DependencyProperty.Register(
                "SelectedMarker",
                typeof (Marker),
                typeof (HowToPlayer),
                new PropertyMetadata(new PropertyChangedCallback(OnSelectedMarkerChanged)));

        public static readonly DependencyProperty VideosProperty =
            DependencyProperty.Register(
                "Videos",
                typeof (IEnumerable),
                typeof (HowToPlayer),
                new PropertyMetadata(new PropertyChangedCallback(OnVideosChanged)));

        public static readonly DependencyProperty MarkersProperty =
            DependencyProperty.Register(
                "Markers",
                typeof(IEnumerable<Marker>),
                typeof(HowToPlayer),
                new PropertyMetadata(new PropertyChangedCallback(OnMarkersChanged)));

        private ItemsControl _videoInfoList;

        public HowToPlayer()
        {
            DefaultStyleKey = typeof (HowToPlayer);
            MarkerData = new MarkerData();
        }

        public IEnumerable Videos
        {
            get { return (IEnumerable) GetValue(VideosProperty); }
            set { SetValue(VideosProperty, value); }
        }

        public Marker SelectedMarker
        {
            get { return (Marker) GetValue(SelectedMarkerProperty); }
            set { SetValue(SelectedMarkerProperty, value); }
        }

        public IEnumerable<Marker> Markers
        {
            get { return (IEnumerable<Marker>) GetValue(MarkersProperty); }
            set { SetValue(MarkersProperty, value); }
        }

        private void LoadVideos(IEnumerable videos)
        {
            if (_videoInfoList != null)
            {
                _videoInfoList.ItemsSource = videos;
            }
        }

        private void LoadMarkers(IEnumerable<Marker> markers)
        {
            this.MarkerData.Markers.Clear();
            markers.ForEach(i => this.MarkerData.Markers.Add(i));
        }

        private void SkipToMarker(Marker marker)
        {
            MediaElement.Position = marker.Time;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _videoInfoList = GetTemplateChild(VideoInfoListElementName) as ItemsControl;
            if (Videos != null && _videoInfoList != null)
            {
                _videoInfoList.ItemsSource = Videos;
            }
        }

        private static void OnSelectedMarkerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var player = d as HowToPlayer;
            if (player != null)
            {
                player.SkipToMarker(player.SelectedMarker);
            }
        }

        private static void OnVideosChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var player = d as HowToPlayer;
            if (player != null)
            {
                player.LoadVideos(player.Videos);
            }
        }

        private static void OnMarkersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var player = d as HowToPlayer;
            if (player != null)
            {
                player.LoadMarkers(player.Markers);
            }
        }
    }
}